#include <stdio.h>
#include <stdlib.h>

typedef struct link
{
	int data;
	struct link *next;
}link_node;

link_node *creat(void);

int tailinsert(link_node *H,int num);
link_node *addlink(link_node *H,link_node *D);

int main(int argc, const char *argv[])
{
	link_node *H=creat();
	link_node *D=creat();
	tailinsert(H,1);
	tailinsert(H,3);
	tailinsert(H,5);
	tailinsert(H,7);
	tailinsert(H,9);
	tailinsert(H,11);
	tailinsert(H,13);
	tailinsert(H,15);

	tailinsert(D,2);
	tailinsert(D,4);
	tailinsert(D,6);
	tailinsert(D,8);
	tailinsert(D,10);
	tailinsert(D,12);
	tailinsert(D,14);



	link_node *P=addlink(H,D);
	while(P->next!=NULL)
	{
		P=P->next;
		printf("%d ",P->data);
	}
	printf("\n");
	while(H->next!=NULL)
	{
		H=H->next;
		printf("%d ",H->data);
	}
	printf("\n");

	while(D->next!=NULL)
	{
		D=D->next;
		printf("%d ",D->data);
	}

	return 0;
}

link_node *creat()
{
	link_node *H=(link_node *)malloc(sizeof(link_node));

	H->data=0;
	H->next=NULL;

	return H;	
}



int tailinsert(link_node *H,int num)
{
	link_node *p=(link_node *)malloc(sizeof(link_node));
	p->data=num;
	while(H->next!=NULL)
	{
		H=H->next;
	}
	p->next=H->next;
	H->next=p;

	return 0;
}

link_node *addlink(link_node *H,link_node *D)
{
	link_node *p=creat();
	H=H->next;
	D=D->next;

	int Hflag=0;
	int Dflag=0;

	while(1)
	{
		if(H==NULL)
		{
			Hflag=1;
			break;
		}
		else if(D==NULL)
		{
			Dflag=1;
			break;
		}

		if(H->data<=D->data)
		{
			tailinsert(p,H->data);
			H=H->next;

		}
		else
		{
			tailinsert(p,D->data);
			D=D->next;
		}

	}	
	printf("H:%d\nD:%d\n",Hflag,Dflag);
	
	if(Hflag!=1)
	{

		while(H!=NULL)
		{
			tailinsert(p,H->data);
			H=H->next;
		}
	}
	if(Dflag!=1)
	{	
		while(D!=NULL)
		{
			tailinsert(p,D->data);
			D=D->next;
		}
	}

	return p;
}


